home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- '''Class representing an X.509 certificate chain.'''
- from utils import cryptomath
-
- class X509CertChain:
- '''This class represents a chain of X.509 certificates.
-
- @type x509List: list
- @ivar x509List: A list of L{tlslite.X509.X509} instances,
- starting with the end-entity certificate and with every
- subsequent certificate certifying the previous.
- '''
-
- def __init__(self, x509List = None):
- '''Create a new X509CertChain.
-
- @type x509List: list
- @param x509List: A list of L{tlslite.X509.X509} instances,
- starting with the end-entity certificate and with every
- subsequent certificate certifying the previous.
- '''
- if x509List:
- self.x509List = x509List
- else:
- self.x509List = []
-
-
- def getNumCerts(self):
- '''Get the number of certificates in this chain.
-
- @rtype: int
- '''
- return len(self.x509List)
-
-
- def getEndEntityPublicKey(self):
- '''Get the public key from the end-entity certificate.
-
- @rtype: L{tlslite.utils.RSAKey.RSAKey}
- '''
- if self.getNumCerts() == 0:
- raise AssertionError()
- self.getNumCerts() == 0
- return self.x509List[0].publicKey
-
-
- def getFingerprint(self):
- '''Get the hex-encoded fingerprint of the end-entity certificate.
-
- @rtype: str
- @return: A hex-encoded fingerprint.
- '''
- if self.getNumCerts() == 0:
- raise AssertionError()
- self.getNumCerts() == 0
- return self.x509List[0].getFingerprint()
-
-
- def getCommonName(self):
- """Get the Subject's Common Name from the end-entity certificate.
-
- The cryptlib_py module must be installed in order to use this
- function.
-
- @rtype: str or None
- @return: The CN component of the certificate's subject DN, if
- present.
- """
- if self.getNumCerts() == 0:
- raise AssertionError()
- self.getNumCerts() == 0
- return self.x509List[0].getCommonName()
-
-
- def validate(self, x509TrustList):
- '''Check the validity of the certificate chain.
-
- This checks that every certificate in the chain validates with
- the subsequent one, until some certificate validates with (or
- is identical to) one of the passed-in root certificates.
-
- The cryptlib_py module must be installed in order to use this
- function.
-
- @type x509TrustList: list of L{tlslite.X509.X509}
- @param x509TrustList: A list of trusted root certificates. The
- certificate chain must extend to one of these certificates to
- be considered valid.
- '''
- import cryptlib_py
- c1 = None
- c2 = None
- lastC = None
- rootC = None
-
- try:
- rootFingerprints = [ c.getFingerprint() for c in x509TrustList ]
- for cert1, cert2 in zip(self.x509List, self.x509List[1:]):
- if cert1.getFingerprint() in rootFingerprints:
- return True
- c1 = cryptlib_py.cryptImportCert(cert1.writeBytes(), cryptlib_py.CRYPT_UNUSED)
- c2 = cryptlib_py.cryptImportCert(cert2.writeBytes(), cryptlib_py.CRYPT_UNUSED)
-
- try:
- cryptlib_py.cryptCheckCert(c1, c2)
- except:
- cert1.getFingerprint() in rootFingerprints
- []
- []
- return False
-
- cryptlib_py.cryptDestroyCert(c1)
- c1 = None
- cryptlib_py.cryptDestroyCert(c2)
- c2 = None
-
- if self.x509List[-1].getFingerprint() in rootFingerprints:
- return True
- lastC = cryptlib_py.cryptImportCert(self.x509List[-1].writeBytes(), cryptlib_py.CRYPT_UNUSED)
- for rootCert in x509TrustList:
- rootC = cryptlib_py.cryptImportCert(rootCert.writeBytes(), cryptlib_py.CRYPT_UNUSED)
- if self._checkChaining(lastC, rootC):
-
- try:
- cryptlib_py.cryptCheckCert(lastC, rootC)
- return True
- cert1.getFingerprint() in rootFingerprints
- []
- return False
-
- continue
- []
-
- return False
- finally:
- if c1 is not None:
- cryptlib_py.cryptDestroyCert(c1)
-
- if c2 is not None:
- cryptlib_py.cryptDestroyCert(c2)
-
- if lastC is not None:
- cryptlib_py.cryptDestroyCert(lastC)
-
- if rootC is not None:
- cryptlib_py.cryptDestroyCert(rootC)
-
-
-
-
- def _checkChaining(self, lastC, rootC):
- import cryptlib_py
- import array
-
- def compareNames(name):
-
- try:
- length = cryptlib_py.cryptGetAttributeString(lastC, name, None)
- lastName = array.array('B', [
- 0] * length)
- cryptlib_py.cryptGetAttributeString(lastC, name, lastName)
- lastName = lastName.tostring()
- except cryptlib_py.CryptException:
- e = None
- if e[0] == cryptlib_py.CRYPT_ERROR_NOTFOUND:
- lastName = None
-
- except:
- e[0] == cryptlib_py.CRYPT_ERROR_NOTFOUND
-
-
- try:
- length = cryptlib_py.cryptGetAttributeString(rootC, name, None)
- rootName = array.array('B', [
- 0] * length)
- cryptlib_py.cryptGetAttributeString(rootC, name, rootName)
- rootName = rootName.tostring()
- except cryptlib_py.CryptException:
- e = None
- if e[0] == cryptlib_py.CRYPT_ERROR_NOTFOUND:
- rootName = None
-
- except:
- e[0] == cryptlib_py.CRYPT_ERROR_NOTFOUND
-
- return lastName == rootName
-
- cryptlib_py.cryptSetAttribute(lastC, cryptlib_py.CRYPT_CERTINFO_ISSUERNAME, cryptlib_py.CRYPT_UNUSED)
- if not compareNames(cryptlib_py.CRYPT_CERTINFO_COUNTRYNAME):
- return False
- if not compareNames(cryptlib_py.CRYPT_CERTINFO_LOCALITYNAME):
- return False
- if not compareNames(cryptlib_py.CRYPT_CERTINFO_ORGANIZATIONNAME):
- return False
- if not compareNames(cryptlib_py.CRYPT_CERTINFO_ORGANIZATIONALUNITNAME):
- return False
- if not compareNames(cryptlib_py.CRYPT_CERTINFO_COMMONNAME):
- return False
- return True
-
-
-